<?php

/**********************************************************************
Copyright (C) FrontAccounting, LLC.
Released under the terms of the GNU General Public License, GPL, 
as published by the Free Software Foundation, either version 3 
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_GLANALYTIC';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	List of Journal Entries
// ----------------------------------------------------------------
$path_to_root = "..";

include_once ($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/includes/date_functions.inc");
include_once ($path_to_root . "/includes/data_checks.inc");
include_once ($path_to_root . "/gl/includes/gl_db.inc");
include_once ($path_to_root . "/includes/ui/ui_view.inc");
include_once ($path_to_root . "/gl/includes/db/gl_db_accounts.inc");

//----------------------------------------------------------------------------------------------------
//$from = sql2date('2012-01-01');
//$to = sql2date('2012-12-31');
//$jml = get_laba_rugi($from,$to);
//return;
print_list_of_journal_entries();

//----------------------------------------------------------------------------------------------------

function print_list_of_journal_entries()
{
    global $path_to_root, $systypes_array;

    $from = $_POST['PARAM_0'];
    $to = $_POST['PARAM_1'];
    $estimasi = input_num('PARAM_2'); 
        $comments = null;
    //	$destination = $_POST['PARAM_4'];
    //if ($destination)
    include_once ($path_to_root . "/reporting/includes/excel_report.inc");
    //	else
    //		include_once($path_to_root . "/reporting/includes/pdf_report.inc");

    $dec = user_price_dec();

    $cols = array(0,100,200,300,400,500,600,700);

    $headers = array('','','Penj Distro','Penj Tunai Selain Kaos','Penj Pesanan','Total Penjualan','% Atas Penjualan');

    $aligns = array('left','left','right','right','right','right','right');

    $params = array(0 => $comments, 1 => array(
            'text' => 'Periode',
            'from' => $from,
            'to' => $to));
	$from = add_days($from, -1);
	$to = add_days($to, 1);
    $rep = new FrontReport('Laporan Laba Rugi', "LaporanLabaRugi", user_pagesize());
    $rep->formatHeaderLeft->setTop(0);
    $rep->formatHeaderRight->setTop(0);                                              
    $rep->formatHeaderLeft->setBottom(2);
    $rep->formatHeaderRight->setBottom(2);                                          
    $rep->SetHeaderType('Header4');
    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();
    
    $total_laba_kotor = 0.0;
    $penjualan = 0.0;
    $return_penjualan = 0.0;
    $hpp = 0.0;
    $hpp_details = 0.0;
    $total_laba_bersih = 0.0;

    //$rep->TextCol(0, 1, get_gl_account_name('41100'));
    $rep->NewLine();
    $rep->TextCol(0, 1, 'Penjualan');
    $penjualan_distro = -(get_gl_balance_from_to($from, $to, '41100', 0, 0));    
    $rep->AmountCol(2, 3, $penjualan_distro, 0);
    $penjualan_tunai_selain_kaos = -(get_gl_balance_from_to($from, $to, '41200', 0, 0));
    $rep->AmountCol(3, 4, $penjualan_tunai_selain_kaos, 0);
    $penjualan_pesanan = -(get_gl_balance_from_to($from, $to, '41300', 0, 0));
    $rep->AmountCol(4, 5, $penjualan_pesanan, 0);
    $total_penjualan = $penjualan_distro + $penjualan_tunai_selain_kaos + $penjualan_pesanan;
    $rep->AmountCol(5, 6, $total_penjualan, 0);
    $penjualan_persen = ($total_penjualan / $total_penjualan) * 100;
    $rep->PercentCol(6, 7, $penjualan_persen, 1);
    
    $rep->NewLine();
    $rep->TextCol(0, 1, 'Retur Penjualan');
    $ret_penjualan_distro = get_gl_balance_from_to($from, $to, '42100', 0, 0);    
    $rep->AmountCol(2, 3, $ret_penjualan_distro, 0);
    $ret_penjualan_tunai_selain_kaos = get_gl_balance_from_to($from, $to, '42200', 0, 0);
    $rep->AmountCol(3, 4, $ret_penjualan_tunai_selain_kaos, 0);
    $ret_penjualan_pesanan = get_gl_balance_from_to($from, $to, '42300', 0, 0);
    $rep->AmountCol(4, 5, $ret_penjualan_pesanan, 0);
    $ret_total_penjualan = $ret_penjualan_distro + $ret_penjualan_tunai_selain_kaos + $ret_penjualan_pesanan;
    $rep->AmountCol(5, 6, $ret_total_penjualan, 0);
    $ret_penjualan_persen = ($ret_total_penjualan / $total_penjualan) * 100;
    $rep->PercentCol(6, 7, $ret_penjualan_persen, 1);
    //$rep->NewLine();
    //$rep->TextCol(0, 1, get_gl_account_name('42100'));
    $rep->NewLine();
    $rep->TextCol(1, 2, 'Penjualan Bersih');
    $penjualan_bersih_distro = $penjualan_distro - $ret_penjualan_distro;    
    $rep->AmountCol(2, 3, $penjualan_bersih_distro, 0);
    $penjualan_bersih_tunai_selain_kaos = $penjualan_tunai_selain_kaos - $ret_penjualan_tunai_selain_kaos;
    $rep->AmountCol(3, 4, $penjualan_bersih_tunai_selain_kaos, 0);
    $penjualan_bersih__pesanan = $penjualan_pesanan - $ret_penjualan_pesanan;
    $rep->AmountCol(4, 5, $penjualan_bersih__pesanan, 0);
    $total_penjualan_bersih = $penjualan_bersih_distro + $penjualan_bersih_tunai_selain_kaos + $penjualan_bersih__pesanan;
    $rep->AmountCol(5, 6, $total_penjualan_bersih, 0);
    $penjualan_bersih_persen = ($total_penjualan_bersih / $total_penjualan) * 100;
    $rep->PercentCol(6, 7, $penjualan_bersih_persen, 1);
    
    $rep->NewLine();
    $format = & $rep->addFormat();
    $format->setBold();
    $format->setAlign('left');
    $format->setAlign('vcenter');
    $rep->TextColExt(0, 1, 'Harga Pokok Penjualan',$format);
    $rep->NewLine();
    
    $hpp_account = get_gl_hpp_details();
    $hpp_penj_pesanan = 0;
    while ($myrow = db_fetch($hpp_account))
    {                           
        $rep->TextCol(1,2, get_gl_account_name($myrow['account_code']));
        $hpp_details = get_gl_balance_from_to($from, $to, $myrow['account_code'], 0, 0);
        $hpp_penj_pesanan += $hpp_details;
        $rep->AmountCol(4, 5, $hpp_details, 0);
        $rep->AmountCol(5, 6, $hpp_details, 0);
        $rep->PercentCol(6, 7, ($hpp_details / $total_penjualan) * 100, 1);
        $rep->NewLine();
    }   
    
    $rep->TextCol(0, 1, 'Harga Pokok Penjualan');
    
    $hpp_distro = get_gl_balance_from_to($from, $to, '51000', 0, 0);
    $rep->AmountCol(2, 3, $hpp_distro, 0);
    $hpp_penj_tunai_selain_kaos = get_gl_balance_from_to($from, $to, '52000', 0, 0);
    $rep->AmountCol(3, 4, $hpp_penj_tunai_selain_kaos, 0);
    $rep->AmountCol(4, 5, $hpp_penj_pesanan, 0);
    $hpp_total = $hpp_distro + $hpp_penj_tunai_selain_kaos + $hpp_penj_pesanan;
    $rep->AmountCol(5, 6, $hpp_total, 0);
    $rep->PercentCol(6, 7, ($hpp_total / $total_penjualan) * 100, 1);
    
    $rep->NewLine();
    $rep->TextCol(1, 2, 'Laba Kotor');
    $laba_kotor_distro = $penjualan_bersih_distro - $hpp_distro;
    $rep->AmountCol(2, 3, $laba_kotor_distro, 0);
    $laba_kotor_penj_selain_kaos = $penjualan_bersih_tunai_selain_kaos - $hpp_penj_tunai_selain_kaos;
    $rep->AmountCol(3, 4, $laba_kotor_penj_selain_kaos, 0);
    $laba_kotor_penj_pesanan = $penjualan_bersih__pesanan - $hpp_penj_pesanan;
    $rep->AmountCol(4, 5, $laba_kotor_penj_pesanan, 0);
    $total_laba_kotor = $laba_kotor_distro + $laba_kotor_penj_selain_kaos + $laba_kotor_penj_pesanan;
    $rep->AmountCol(5, 6, $total_laba_kotor, 0);
    $rep->PercentCol(6, 7, ($total_laba_kotor / $total_penjualan) * 100, 1);
        
    $rep->NewLine();
    $rep->NewLine();
    $beban_pemasaran = 0.0;
    $total_beban_pemasaran = 0.0;
    $beban_pemasaran_account = get_gl_beban_pemasaran();
    while ($myrow = db_fetch($beban_pemasaran_account))
    {    
        $rep->TextCol(0, 1, get_gl_account_name($myrow['account_code']));
        $beban_pemasaran = get_gl_balance_from_to($from, $to, $myrow['account_code'], 0,
            0);
        $total_beban_pemasaran += $beban_pemasaran;
        $rep->AmountCol(5, 6, $beban_pemasaran, 0);
        $rep->PercentCol(6, 7, ($beban_pemasaran / $total_penjualan) * 100, 1);
        $rep->NewLine();
    }
    $rep->TextCol(1, 2, 'Total Beban Pemasaran');
    $rep->AmountCol(5, 6, $total_beban_pemasaran, 0);
    $rep->PercentCol(6, 7, ($total_beban_pemasaran / $total_penjualan) * 100, 1);
    $rep->NewLine();
    $rep->NewLine();
    $beban_adm_umum = 0.0;
    $total_beban_adm_umum = 0.0;
    $beban_adm_umum_account = get_gl_beban_adm_umum();
    while ($myrow = db_fetch($beban_adm_umum_account))
    {           
        $rep->TextCol(0, 1, get_gl_account_name($myrow['account_code']));
        $beban_adm_umum = get_gl_balance_from_to($from, $to, $myrow['account_code'], 0,
            0);
        $total_beban_adm_umum += $beban_adm_umum;
        $rep->AmountCol(5, 6, $beban_adm_umum, 0);
        $rep->PercentCol(6, 7, ($beban_adm_umum  / $total_penjualan) * 100, 1);
        $rep->NewLine();
    }
    $rep->TextCol(1, 2, 'Total Beban Administrasi dan Umum');
    $rep->AmountCol(5, 6, $total_beban_adm_umum, 0);
    $rep->PercentCol(6, 7, ($total_beban_adm_umum / $total_penjualan) * 100, 1);
    $rep->NewLine();
    $rep->NewLine();

    $rep->TextCol(0, 1, get_gl_account_name('41900'));
    $total_penghasilan_lain = -(get_gl_balance_from_to($from, $to, '41900', 0, 0));
    $rep->AmountCol(5, 6, $total_penghasilan_lain, 0);
    $rep->PercentCol(6, 7, ($total_penghasilan_lain / $total_penjualan) * 100, 1);
    $rep->NewLine();
    $rep->NewLine();

    $rep->TextCol(0, 1, 'Laba Bersih');
    $laba_bersih = $total_laba_kotor - $total_beban_pemasaran - $total_beban_adm_umum +
        $total_penghasilan_lain;
    $rep->AmountCol(5, 6, $laba_bersih, 0);
    $rep->PercentCol(6, 7, ($laba_bersih / $total_penjualan) * 100, 1);
    $rep->NewLine();
  
    $date = date_create(date2sql($to));        
    $rep->TextCol(0, 1, 'Estimasi Persediaan Bahan Baku Per '.date_format($date, 'd F Y'));
    $rep->AmountCol(5, 6, $estimasi, 0);
    $rep->PercentCol(6, 7, ($estimasi / $total_penjualan) * 100, 1);
    $rep->NewLine();
    
    $laba_bersih_setelah_penyesuaian = $laba_bersih + $estimasi;
    $rep->TextCol(0, 1, 'Laba Bersih Setelah Penyesuaian Stok Akhir');
    $rep->AmountCol(5, 6, $laba_bersih_setelah_penyesuaian, 0);
    $rep->PercentCol(6, 7, ($laba_bersih_setelah_penyesuaian / $total_penjualan) * 100, 1);
    
    $rep->End();
}

?>